package com.one2manycall.service.impl;

import com.one2manycall.service.UserOnlineManageServiceI;
import com.one2manycall.vo.User;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserOnlineManageServiceImpl  implements UserOnlineManageServiceI {




    @Override
    public void register(User user) {

        onlineUsers.putIfAbsent(user.getUserName(),user);

        onlineTokenUsers.putIfAbsent(user.getToken(),user);

    }

    @Override
    public User getUserById(Long id) {

        Optional<User> first = onlineUsers.values().stream().filter(a -> id == (a.getId())).findFirst();

        User user = first.get();

        return user;

    }

    @Override
    public List<User> getAllUsers() {
        List<User> collect = onlineUsers.values().stream().collect(Collectors.toList());

        return collect;
    }



    @Override
    public void deleteUser(String userName) {
        onlineUsers.remove(userName);
    }

    @Override
    public void deleteUser(Long id) {

    }

    @Override
    public User findByUsername(String name) {
        User user = onlineUsers.get(name);

        return user;
    }

    @Override
    public User findByUserToken(String token) {

        User user = onlineTokenUsers.get(token);
        return user;
    }
}
